package com.bsg.upm.util;

import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

import org.apache.commons.lang.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.bsg.upm.cache.DictTypeCache;
import com.bsg.upm.constant.TaskDictTypeConstants;
import com.bsg.upm.entity.TaskEntity;
import com.bsg.upm.util.SpringContextUtils;

public class LogUtils {

	public static String formate(TaskEntity task, String subActionDescription) {
		DictTypeCache dictTypeCache = (DictTypeCache) SpringContextUtils.getBeanById("dictTypeCache");
		String moduleName = dictTypeCache.getTextFromCache(TaskDictTypeConstants.TASK_OBJ_TYPE, task.getObjType());
		String actionName = dictTypeCache.getTextFromCache(TaskDictTypeConstants.TASK_ACTION_TYPE,
				task.getActionType());
		String targetName = task.getObjName();
		String username = task.getOperateLog().getCreateUserLoginName();
		return formate(username, actionName, moduleName, targetName, subActionDescription);
	}

	private static String formate(String username, String action, String objType, String targetName, String subAction) {
		String ret = "【[ " + username + " ] - [ " + action + " ] - [ " + objType + " ]( " + targetName + " )";
		if (StringUtils.isNotBlank(subAction)) {
			ret = ret + " - [ " + subAction + " ]";
		}
		ret = ret + "】";
		return ret;
	}

	public static String formateUpdate(String tableName, String[] columnNames, Object obj, String[] fieldNames) {
		if (tableName.endsWith("表")) {
			tableName = tableName.substring(0, tableName.length() - 1);
		}
		String ret = "【更新" + tableName + "表】-【更新" + tableName + Arrays.toString(columnNames) + ":("
				+ toString(obj, fieldNames) + ")】";
		return ret;
	}

	private static String toString(Object o, String[] fieldNames) {
		JSONObject json = new JSONObject(true);
		for (String fieldName : fieldNames) {
			json.put(fieldName, getFieldValue(o, fieldName));
		}
		return JSONObject.toJSONString(json, SerializerFeature.WriteMapNullValue);
	}

	private static Field getDeclaredField(Object object, String fieldName) {
		Field field = null;
		Class<?> clazz = object.getClass();
		for (; clazz != Object.class; clazz = clazz.getSuperclass()) {
			try {
				field = clazz.getDeclaredField(fieldName);
				return field;
			} catch (Exception e) {
			}
		}
		return null;
	}

	private static Object getFieldValue(Object object, String fieldName) {
		// 根据 对象和属性名通过取 Field对象
		Field field = getDeclaredField(object, fieldName);
		if (field == null) {
			return null;
		}
		// 抑制Java对其的检查
		field.setAccessible(true);
		try {
			String type = field.getType().toString();// 得到此属性的类型
			if (type.endsWith("Date")) {
				return dateToString((Date) field.get(object));
			} else {
				return field.get(object);
			}
			// 获的属性值

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	private static String dateToString(Date date) {
		if (date == null) {
			return "";
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(date);
	}
}
